/*
 * Copyright 2008 Marcos Silva Pereira
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.grails.plugins.envers

import com.google.code.grails.plugins.envers.entities.*

/**
 * @author marcos.silva@gmail.com
 */
class AuditedEntityDetectorTests extends GroovyTestCase {
	
	def detector
	
	public void setUp() {
		
		super.setUp()
		this.detector = new AuditedEntityDetector()
		
	}
	
	public void testEntitiesMarkedWithAuditMustBeDetectedAsAudited() {
		
		FakeAuditedEntity.audited = true
		
		assertTrue(
			'FakeAuditedEntity must be detected as audited', 
			detector.isAudited(FakeAuditedEntity)
		)
		
	}
	
	public void testEntitiesNotMarkedWithAuditMustNotBeDetectedAsAudited() {
		assertFalse(
			'Classes without audited static property are not audited', 
			detector.isAudited(String)
		)
	}
	
	public void testEntitiesMarkedWithAuditedEqualsTrueMustBeDetectedAsAudited() {
		
		FakeAuditedEntity.audited = true
		
		assertTrue(
			'When audited property is true, entity is detected as audited',
			detector.isAudited(FakeAuditedEntity)
		)
		
	}
	
	public void testEntitiesMarkedWithAuditedEqualsFalseMustNotBeDetectedAsAudited() {
		
		FakeAuditedEntity.audited = false
		
		assertFalse(
			'When audited property is false, entity is not detected as audited',
			detector.isAudited(FakeAuditedEntity)
		)
	}
	
	public void testEntitiesDeclaringAuditedAsAListMustBeRecognizedAsAudited() {
		
		FakeAuditedEntity.audited = ['a', 'list', 'of', 'properties']
		
		assertTrue(
			'When audited is a list, entity is detected as audited',
			detector.isAudited(FakeAuditedEntity)
		)
	}
	
	public void testWhenAuditedIsAEmptyListEntityMustNotBeRecognizedAsAudited() {
		
		FakeAuditedEntity.audited = []
		
		assertFalse(
			'When audited list is empty, entity is not detected as audited', 
			detector.isAudited(FakeAuditedEntity)
		)
	}
	
	public void tearDown() {
		
		super.tearDown()
		this.detector = null
	}
	
}
